package toledo.post;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.FilterOperator;
import com.google.gdata.data.docs.DocumentListEntry;
import com.google.gdata.data.docs.DocumentListFeed;

import toledo.rest.Course;
import toledo.rest.Update;
import toledo.util.DocumentList;

/**
 * NO NEED!
 * 
 * @author Brian
 */
public class AddFileServlet extends HttpServlet
{
	private static final long serialVersionUID=1L;
	
	private void createFileEntities(String fol_id,String course_id)
	{
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
		DocumentList dl = DocumentList.getService();
		try
		{
			DocumentListFeed dlf = dl.getFolderDocsListFeed(fol_id);
			
			for(DocumentListEntry dle:dlf.getEntries())
			{
				List<Entity> files = datastore.prepare(new Query("files").addFilter("res_id",FilterOperator.EQUAL,dle.getResourceId())).asList(FetchOptions.Builder.withDefaults());
				if(files.size()==0)
				{
					Entity file = new Entity("files");
					file.setProperty("filename",dle.getTitle().getPlainText());
					file.setProperty("type",dle.getType());
					file.setProperty("last_modified",dle.getUpdated().toString());
					file.setProperty("href",dle.getDocumentLink().getHref());
					file.setProperty(Course.COURSE_ID,course_id);
					datastore.put(file);
					
					Entity update = new Entity("updates");
					update.setProperty(Update.COURSE_ID,course_id);
					update.setProperty(Update.ENTRY_ID,KeyFactory.keyToString(file.getKey()));
					update.setProperty(Update.TYPE,Update.TYPE_DOCUMENT);
					datastore.put(update);
				}
				else
				{
					Entity file = files.get(0);
					if(!file.getProperty("last_modified").toString().equals(dle.getUpdated().toString()))
					{
						file.setProperty("last_modified",dle.getUpdated().toString());
						datastore.put(file);
						
						Entity update = new Entity("updates");
						update.setProperty(Update.COURSE_ID,course_id);
						update.setProperty(Update.ENTRY_ID,KeyFactory.keyToString(file.getKey()));
						update.setProperty(Update.TYPE,Update.TYPE_DOCUMENT);
						datastore.put(update);
					}
				}
			}
		}
		catch(Exception e)
		{
		}
	}
	public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException
	{
		List<Entity> courses = DatastoreServiceFactory.getDatastoreService().prepare(new Query(Course.DATABASE)).asList(FetchOptions.Builder.withDefaults());
		for(Entity course:courses)
			createFileEntities(course.getProperty(Course.DOCS_RES_ID).toString(),KeyFactory.keyToString(course.getKey()));
	}
}
